/************************************
* Purpose: 主要结果汇报表——基于时间
* Date:  20230508
*************************************/

%macro genReport(indata 	    /*Input data*/
			     ,grp       	 /*group*/
				 ,time       	 /*Time variable*/
				 ,id         	 /*identifier*/
				 ,var        	 /*Outcome*/
				 ,level=1        /*Treat level*/ 
				 ,mar=0          /*margin in non*/
				 ,side=u       	 /*Side=L or U*/
				 ,alpha=0.025   /*alpha*/
				 ,file=          /*filepath*/
				 ,title=主要结果         /*title*/
				 )/des="generate primary results";
	ods escapechar="^";
	proc format;
		value lbl
			1="试验组"
			0="对照组";
	run;
	data __temp;
		set &indata;
		if cats(&grp)="&level" then do;
			group=1;
		end;
		else if ~missing(&grp) then group=0;
	run;
	proc sort data=__temp;
		by &grp. &id. &time.;
	run;
	proc sql noprint;
		select distinct &time into :levelsoftm separated by " "
		from &indata;
		select count(distinct &time) into :numlevels
		from &indata;
	quit;
	data __temp;
		do until(last.&id);
			set __temp;
			by &grp. &id;
			if first.&id then base=&var;
			diff=&var.-base;
			output;
		end;
		keep &grp. &time &id &var diff base;
	run;
	proc means data=__temp;
		class &grp &time;
		var &var diff;
		output out=summary(where=(_type_=3)) mean(&var diff)=mean_&var mean_diff
						   uclm(&var diff)=uclm_&var uclm_diff
						   lclm(&var diff)=lclm_&var lclm_diff;
	run;
	data summary;
		set summary;
		by &grp;
		length mlu1 mlu2 $100;
		mlu1=cats(put(mean_&var,8.2),"(",put(lclm_&var,8.2),",",put(uclm_&var,8.2),")");
		mlu2=cats(put(mean_diff,8.2),"(",put(lclm_diff,8.2),",",put(uclm_diff,8.2),")");
		if first.&grp then call missing(mlu2);
		dummy=1;
		drop mean_: lclm_: uclm_: _type_ _freq_;
	run;
	%do i=2 %to &numlevels;
		ods output TTests=_test_&i  ConfLimits=_cl_&i Equality=eq;
		proc ttest data=__temp h0=&mar side=&side alpha=&alpha;
			where cats(&time)="%scan(&levelsoftm,&i)";
			class group;
			var diff;
		run;
		ods output close;
		data _null_;set eq;call symputx("vareq",probf);run;
		%if &vareq. >= 0.05 %then %do;
			data __visit_&i;
				merge _test_&i(where=(variances="等于")) 
					 _cl_&i(where=(variances="等于") keep=variances mean LowerCLMean UpperCLMean);
				&time=&i;
				drop Variable--DF;
			run; 
		%end;%else %do;
			data __visit_&i;
				merge _test_&i(where=(variances="不等于")) 
					 _cl_&i(where=(variances="不等于") keep=variances mean LowerCLMean UpperCLMean);
				&time=&i;
				drop Variable--DF;
			run; 
		%end;
	%end;
	data _visit;
		set __visit_:;
		mlu3=cats(put(mean,8.2),"(",put(LowerCLMean,8.2),",",put(UpperCLMean,8.2),")");
		mlu3=transtrn(mlu3,'I','Inf');
		mlu3=transtrn(mlu3,'M','-Inf');
		keep &time mlu3 probt;
	run;
	proc sort data=_visit;
		by &time;
	proc sort data=summary;
		by &time;
	run;
	data summary;
		merge summary(in=a) _visit(in=b);
		by &time;
		if a;
	run;
	options nodate nonumber;
	%if %length(&file)>0 %then %do;
		ods tagsets.rtf file="&file" style=journal3a;
	%end;
	title j=center "&title";
	proc report data=summary missing style(report)={width=99%}
		headline  ;
		col time group,(mlu1  mlu2 dummy) ("非劣效" probt mlu3);
		define time/ group "随访期";
		define group/ across "" right f=lbl. order=formatted;
		define mlu1 /group "测量值" right style(header)={bordertopwidth=1.5};
		define mlu2 /group "较基线的差值" right style(header)={bordertopwidth=1.5};
		define dummy/analysis noprint;
		define probt /group "^{style [FONTSTYLE=ITALIC] P}" right style(header)={asis=yes};
		define mlu3 /group "Mean(%sysevalf(100-%sysevalf(&alpha*100))%nrquote(%)CI)" right;
	run;
	%if %length(&file)>0 %then %do;
		ods tagsets.rtf close;
	%end;
	title;
	options date number;
	proc datasets lib=work nodetails nolist nowarn;
		delete _cl_: _test_: eq summary __VISIT_: __temp _visit;
	quit;
%mend;